<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/19 0019
 * Time: 15:34
 */
namespace  Bt;
class BtApi
{
    private $bt_key = "";
    private $bt_server = "";

    private $api_map = [
        1001    => '/system?action=GetSystemTotal',     //获取系统基础统计
        1002    => '/system?action=GetDiskInfo',        //获取磁盘分区信息
        1003    => '/system?action=GetNetWork',         //获取实时状态信息(CPU、内存、网络、负载)
        1004    => '/config?action=get_config',         //获取全局配置
        2001    => '/ajax?action=GetTaskCount',         //检查是否有安装任务
        2002    => '/ajax?action=UpdatePanel',          //检查面板更新
        3001    => '/data?action=getData&table=sites',  //获取网站列表
        3002    => '/site?action=get_site_types',       //获取网站分类
        3003    => '/site?action=GetPHPVersion',        //获取已安装的 PHP 版本列表,
        3004    => '/site?action=AddSite',              //创建网站
        3005    => '/site?action=DeleteSite',           //删除网站
        3007    => '/site?action=SiteStop',             //停用网站
        3008    => '/site?action=SiteStart',            //启用网站
        3009    => '/site?action=SetEdate',             //网站到期时间
        3010    => '/data?action=setPs&table=sites',    //修改网站备注,
        3011    => '/data?action=getData&table=backup', //获取网站备份列表
        3012    => '/site?action=ToBackup',             //创建网站备份
        3013    => '/site?action=DelBackup',            //删除网站备份
        3014    => '/data?action=getData&table=domain', //获取网站域名列表
        3015    => '/site?action=AddDomain',            //添加网站域名
        3016    => '/site?action=DelDomain',            //删除域名
        3017    => '/site?action=GetRewriteList',       //获取可选的预定义伪静态列表
        3018    => '/files?action=GetFileBody',         //获取指定预定义伪静态规则内容(获取文件内容)
        3019    => '/files?action=SaveFileBody',        //保存伪静态规则内容(保存文件内容)
    ];

    /**
     *  实例化
     * @param  string $server 服务器地址,必须带协议头http或https
     * @param  string $key 服务器API 密钥
     * @param  int          宝塔端口号
    */
    public function __construct($server,$key,$port = 8888)
    {
        $this->bt_server = $server . ":" . $port;
        $this->bt_key = $key;
    }

    /**
     *  构建POST必要数据
    */
    private function postData(){
        $now = time();
        return [
            'request_token'	=>	md5($now.''.md5($this->bt_key)),
            'request_time'	=>	$now
        ];
    }

    /**
     * 发起POST请求
     * @param String $url 目标网填，带http://
     * @param Array|String $data 欲提交的数据
     * @return string
     */
    private function request($url, $data,$timeout = 60)
    {

        //定义cookie保存位置
        $cookie_fold= RUNTIME_PATH . "cookie" . DS  ;

        if(!file_exists($cookie_fold)){
            @mkdir($cookie_fold);
        }

        $cookie_file = $cookie_fold . md5($this->bt_server).'.cookie';
        if(!file_exists($cookie_file)){
            $fp = fopen($cookie_file,'w+');
            fclose($fp);
        }

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
        curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }

    /**
     * 获取数据
    */
    public  function get($api_code , $params = []){
        if(!isset($this->api_map[$api_code])){
            return -1;
        }

        $url = $this->bt_server . $this->api_map[$api_code];
        $postData = $this->postData();
        $postData = array_merge($postData , $params);
        //echo $this->request("http://api.lookbywhile.com/v1/index/data" ,[]);

        return  $this->request($url , $postData);
    }
}